package com.gzcstec.user.controller;

import com.gzcstec.user.constant.CookieConstants;
import com.gzcstec.user.constant.RedisConstants;
import com.gzcstec.user.dataobject.UserInfo;
import com.gzcstec.user.enums.ResultEnums;
import com.gzcstec.user.enums.UserTypeEnums;
import com.gzcstec.user.service.UserService;
import com.gzcstec.user.utils.CookieUtils;
import com.gzcstec.user.utils.RedisUtils;
import com.gzcstec.user.utils.ResultUtils;
import com.gzcstec.user.vo.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 用户服务
 */
@RestController
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisUtils redisUtils;

    @GetMapping("/buyer")
    public ResultVO buyer(@RequestParam("openid") String openid,
                          HttpServletResponse response,
                          HttpServletRequest request) {
        UserInfo userInfo = userService.findByOpenId(openid);
        //检查用户是否存在
        if(null == userInfo) {
            return ResultUtils.error(ResultEnums.LOGIN_FAIL);
        }
        //检查用户类别
        if(UserTypeEnums.BUYER.getCode() != userInfo.getUserType()) {
            return ResultUtils.error(ResultEnums.ROLL_ERROR);
        }
        //设置cookie
        CookieUtils.set(response , CookieConstants.OPENID, openid , CookieConstants.expire);

        return ResultUtils.success();
    }

    @GetMapping("/seller")
    public ResultVO seller(@RequestParam("openid") String openid,
                          HttpServletResponse response,
                           HttpServletRequest request) {
        //判断是否存在cookie，是否存在token，如果有直接返回成功
        Cookie cookie = CookieUtils.get(request , CookieConstants.TOKEN);
        if(cookie != null) {
            //判断redis是否存在openid
            if(!StringUtils.isEmpty(redisUtils.get(String.format(RedisConstants.TOKEN_TEMPLATE , cookie.getValue())))) {
                return ResultUtils.success();
            }
        }

        UserInfo userInfo = userService.findByOpenId(openid);
        //检查用户是否存在
        if(null == userInfo) {
            return ResultUtils.error(ResultEnums.LOGIN_FAIL);
        }
        //检查用户类别
        if(UserTypeEnums.SELLER.getCode() != userInfo.getUserType()) {
            return ResultUtils.error(ResultEnums.ROLL_ERROR);
        }
        //设置cookie token=xyz，redis token=openid
        String token = UUID.randomUUID().toString();
        CookieUtils.set(response , CookieConstants.TOKEN, token , CookieConstants.expire);
        //设置redis
        redisUtils.set(String.format(RedisConstants.TOKEN_TEMPLATE , token)
                , openid
                , CookieConstants.expire
                , TimeUnit.SECONDS);

        return ResultUtils.success();
    }

}
